package hu.plusone.eregister.service;

import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

public class DbFactory {

    private final String host;
    private final int port;
    private final String database;
    private final String user;
    private final String password;

    public DbFactory(String host, int port, String database, String user, String password) {
        this.host = host;
        this.port = port;
        this.database = database;
        this.user = user;
        this.password = password;
    }

    public MongoDbFactory mongoDbFactory() {
        try {
            MongoCredential mc = MongoCredential.createMongoCRCredential(user, database, password.toCharArray());
            MongoClient mongoClient = new MongoClient(new ServerAddress(host, port), Arrays.asList(mc));
            return new SimpleMongoDbFactory(mongoClient, database);
        } catch (UnknownHostException ex) {
            throw new RuntimeException(ex);
        }
    }
}
